using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Drawing.Drawing2D;
using System.IO;

namespace GDIdotNETgames
{
    public partial class UserControlMap : UserControl
    {
        private CustomControlMap _customControlMapMain = null;

        public UserControlMap()
        {
            InitializeComponent();
            MouseWheel += new MouseEventHandler(UserControlMap_MouseWheel);
            Resize += new EventHandler(UserControlMap_Resize);
            hScrollBarMap.ValueChanged += new EventHandler(hScrollBarMap_ValueChanged);
            vScrollBarMap.ValueChanged += new EventHandler(vScrollBarMap_ValueChanged);
            vScrollBarMapZoom.ValueChanged += new EventHandler(vScrollBarMapZoom_ValueChanged);
        }

        public void LoadFromXML(XmlReader reader)
        {
            if (_customControlMapMain != null)
            {
                panelMapScroll.Controls.Remove(_customControlMapMain);
                _customControlMapMain.Dispose();
            }
            _customControlMapMain = new CustomControlMap(
                new Rectangle(
                    new Point(3, 3),
                    new Size(panelMapScroll.Width - 27, panelMapScroll.Height - 27)),
                reader);
            panelMapScroll.Controls.Add(_customControlMapMain);
            vScrollBarMapZoom.Maximum = (int)(100 * _customControlMapMain.MaximumZoom);
            _customControlMapMain.Zoom(vScrollBarMapZoom.Value / 100f);
            hScrollBarMap.Minimum = 0;
            hScrollBarMap.Maximum = _customControlMapMain.MaximumScroll.X;
            vScrollBarMap.Minimum = 0;
            vScrollBarMap.Maximum = _customControlMapMain.MaximumScroll.Y;
            _customControlMapMain.Scroll(new PointF(hScrollBarMap.Value, vScrollBarMap.Value));
            _customControlMapMain.MapAutoScroll += new CustomControlMap.MapAutoScrollEventHandler(_customControlMapMain_MapAutoScroll);
        }

        public void SerializeToXML(XmlWriter writer)
        {
            if (_customControlMapMain != null)
                _customControlMapMain.SerializeToXML(writer);
        }

        void _customControlMapMain_MapAutoScroll(object sender, MapAutoScrollEventArgs mase)
        {
            if (_customControlMapMain != null)
            {
                hScrollBarMap.Value = Math.Min(hScrollBarMap.Maximum, Math.Max(hScrollBarMap.Minimum, hScrollBarMap.Value + mase.ScrollVector.X));
                vScrollBarMap.Value = Math.Min(vScrollBarMap.Maximum, Math.Max(vScrollBarMap.Minimum, vScrollBarMap.Value + mase.ScrollVector.Y));
            }
        }

        void UserControlMap_MouseWheel(object sender, MouseEventArgs e)
        {
            vScrollBarMapZoom.Value = Math.Min(vScrollBarMapZoom.Maximum, Math.Max(vScrollBarMapZoom.Minimum,
                vScrollBarMapZoom.Value - vScrollBarMapZoom.Minimum * e.Delta / 120));
        }

        void vScrollBarMapZoom_ValueChanged(object sender, EventArgs e)
        {
            if (_customControlMapMain != null)
            {
                _customControlMapMain.Zoom(vScrollBarMapZoom.Value / 100f);
                hScrollBarMap.Minimum = 0;
                hScrollBarMap.Maximum = _customControlMapMain.MaximumScroll.X;
                vScrollBarMap.Minimum = 0;
                vScrollBarMap.Maximum = _customControlMapMain.MaximumScroll.Y;
                _customControlMapMain.Scroll(new PointF(hScrollBarMap.Value, vScrollBarMap.Value));
            }
        }

        void vScrollBarMap_ValueChanged(object sender, EventArgs e)
        {
            if (_customControlMapMain != null)
                _customControlMapMain.Scroll(new PointF(hScrollBarMap.Value, vScrollBarMap.Value));
        }

        void hScrollBarMap_ValueChanged(object sender, EventArgs e)
        {
            if (_customControlMapMain != null)
                _customControlMapMain.Scroll(new PointF(hScrollBarMap.Value, vScrollBarMap.Value));
        }

        void UserControlMap_Resize(object sender, EventArgs e)
        {
            if (_customControlMapMain != null)
            {
                vScrollBarMapZoom.Maximum = (int)(100 * _customControlMapMain.MaximumZoom);
                _customControlMapMain.Zoom(vScrollBarMapZoom.Value / 100f);
                hScrollBarMap.Minimum = 0;
                hScrollBarMap.Maximum = _customControlMapMain.MaximumScroll.X;
                vScrollBarMap.Minimum = 0;
                vScrollBarMap.Maximum = _customControlMapMain.MaximumScroll.Y;
                _customControlMapMain.Scroll(new PointF(hScrollBarMap.Value, vScrollBarMap.Value));
            }
        }
    }
}